﻿using EMRView;
using HC.View;
using EMRView.Extensions.Command;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using EMRView.Extensions.Command.StandardCommand.Insert;

namespace EMRView.Extensions.Extension
{
    /// <summary>
    /// 编辑器扩展函数
    /// </summary>
    public static class HCEmrViewExtension
    {
        public static object ExecuteCommand(this HCEmrView view, string commandName, bool showUI, object param)
        {
            return HCCommandControl.ExecuteCommand(view, commandName, showUI, param);
        }

        #region 获取编辑器中的元素

        /// <summary>
        /// 获取所有Index为deIndex得元素
        /// </summary>
        /// <param name="view"></param>
        /// <param name="deIndex"></param>
        /// <returns>返回基类HCCustomItem得集合</returns>
        public static List<HCCustomItem> GetHCCustomItemsByIndex(this HCEmrView view, string deIndex)
        {
            HCItemTraverse vItemTraverse = new HCItemTraverse();//准备存放遍历信息的对象
            vItemTraverse.Areas.Add(SectionArea.saHeader);
            vItemTraverse.Areas.Add(SectionArea.saPage);//遍历正文中的信息
            vItemTraverse.Areas.Add(SectionArea.saFooter);

            List<HCCustomItem> vItems = new List<HCCustomItem>();
            TraverseItemEventHandle vTraveEvent = delegate (HCCustomData aData, int aItemNo, int aTag, Stack<HCDomainNode> aDomainStack, ref bool aStop)
            {
                var vItem = aData.Items[aItemNo];
                if ((vItem is DeItem) && (vItem as DeItem)[DeProp.Index] == deIndex)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeEdit) && (vItem as DeEdit)[DeProp.Index] == deIndex)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeCheckBox) && (vItem as DeCheckBox)[DeProp.Index] == deIndex)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeGroup) && (vItem as DeGroup)[DeProp.Index] == deIndex)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeImageItem) && (vItem as DeImageItem)[DeProp.Index] == deIndex)
                {
                    vItems.Add(vItem);
                }
            };

            vItemTraverse.Process = vTraveEvent;//遍历到每一个文本对象是触发的事件
            view.TraverseItem(vItemTraverse);//开始遍历

            return vItems;
        }

        /// <summary>
        /// 获取所有Name属性为name得元素
        /// </summary>
        /// <param name="view"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static List<HCCustomItem> GetHCCustomItemsByName(this HCEmrView view, string name)
        {
            HCItemTraverse vItemTraverse = new HCItemTraverse();//准备存放遍历信息的对象
            vItemTraverse.Areas.Add(SectionArea.saHeader);
            vItemTraverse.Areas.Add(SectionArea.saPage);//遍历正文中的信息
            vItemTraverse.Areas.Add(SectionArea.saFooter);

            List<HCCustomItem> vItems = new List<HCCustomItem>();
            TraverseItemEventHandle vTraveEvent = delegate (HCCustomData aData, int aItemNo, int aTag, Stack<HCDomainNode> aDomainStack, ref bool aStop)
            {
                var vItem = aData.Items[aItemNo];
                if ((vItem is DeItem) && (vItem as DeItem)[DeProp.Name] == name)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeEdit) && (vItem as DeEdit)[DeProp.Name] == name)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeCheckBox) && (vItem as DeCheckBox)[DeProp.Name] == name)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeGroup) && (vItem as DeGroup)[DeProp.Name] == name)
                {
                    vItems.Add(vItem);
                }
                if ((vItem is DeImageItem) && (vItem as DeImageItem)[DeProp.Name] == name)
                {
                    vItems.Add(vItem);
                }
            };

            vItemTraverse.Process = vTraveEvent;//遍历到每一个文本对象是触发的事件
            view.TraverseItem(vItemTraverse);//开始遍历

            return vItems;
        }

        /// <summary>
        /// 获取指定DeGroup的内容
        /// </summary>
        /// <param name="group"></param>
        /// <param name="deIndex">获取DeGroup的Text</param>
        /// <param name="view">当前DeGroup所在的编辑器</param>
        /// <returns></returns>
        public static string GetDeGroupText(this HCCustomItem item, string deIndex, HCEmrView view)
        {
            string result = "";
            if (item is DeGroup)
            {
                int startNo = 0;
                int endNo = 0;
                view.GetDataDeGroupItemNo(view.ActiveSection.Page, deIndex, false, ref startNo, ref endNo);
                result = view.GetDataDeGroupText(view.ActiveSection.Page, startNo, view.ActiveSection.Page.GetDomainAnother(startNo));
            }
            else if (item is DeItem)
            {
                view.GetDeItemText(deIndex, ref result);
            }
            else if (item is DeEdit)
            {

            }
            return result;
        }

        #endregion

        #region 获取和加载XML函数

        /// <summary>
        /// 获取当前编辑器文档得XML文档内容
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public static string GetXmlString(this HCEmrView view)
        {
            using (Stream stream = new MemoryStream())
            {
                view.SaveToXmlStream(stream, Encoding.UTF8);
                byte[] xmlByte = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(xmlByte, 0, xmlByte.Length);
                stream.Close();
                return System.Text.Encoding.UTF8.GetString(xmlByte);
            }
        }

        /// <summary>
        /// 加载XML字符串
        /// </summary>
        /// <param name="view"></param>
        /// <param name="xml"></param>
        public static void LoadXmlString(this HCEmrView view, string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                throw new ArgumentException("xml无效");
            }
            using (Stream stream = new MemoryStream())
            {
                var xmlByte = System.Text.Encoding.UTF8.GetBytes(xml);
                stream.Seek(0, SeekOrigin.Begin);
                stream.Write(xmlByte, 0, xmlByte.Length);
                stream.Seek(0, SeekOrigin.Begin);
                view.LoadFromXmlStream(stream);
                view.IsChanged = false;
                stream.Close();
            }
        }

        #endregion

        #region 添加扩展函数

        /// <summary>
        /// 添加可编辑元素
        /// </summary>
        /// <param name="view"></param>
        /// <param name="setting"></param>
        public static void AddEdit(this HCEmrView view, DeEditSetting setting = null)
        {
            if (setting == null)
            {
                setting = new DeEditSetting();
            }
            DeEdit deEdit = new DeEdit(view.ActiveSectionTopLevelData(), "");
            HC.View.BorderSide side = 0;
            if (setting.IsBottomBorder)
            {
                side = side | HC.View.BorderSide.cbsBottom;
            }
            if (setting.IsLeftBorder)
            {
                side = side | HC.View.BorderSide.cbsLeft;
            }
            if (setting.IsRightBorder)
            {
                side = side | HC.View.BorderSide.cbsRight;
            }
            if (setting.IsTopBorder)
            {
                side = side | HC.View.BorderSide.cbsTop;
            }
            deEdit.BorderSides.Value = (byte)(side);
            deEdit.PrintOnlyText = setting.PrintOnlyText;//设置打印边框 true表示只打印文字，fasle表示打印文字的边框样式
            deEdit[DeProp.Index] = setting.EIndex;
            deEdit[DeProp.Name] = setting.EName;
            deEdit.Width = setting.EWidth;
            view.InsertItem(deEdit);
        }

        /// <summary>
        /// 添加输入域
        /// </summary>
        /// <param name="view"></param>
        public static void AddDomain(this HCEmrView view, DeGroupSetting setting = null)
        {
            view.Focus();
            using (DeGroup vDeGroup = new DeGroup(view.ActiveSectionTopLevelData()))
            {
                vDeGroup[DeProp.Index] = setting.EIndex;
                vDeGroup[DeProp.Name] = setting.EName;
                
                view.InsertDeGroup(vDeGroup);
            }
        }

        #endregion
    }
}
